package com.smbms.controller;

import com.smbms.pojo.User;
import com.smbms.service.UserService;
import com.smbms.util.JwtUtil;
import com.smbms.util.PageResult;
import com.smbms.util.Result;
import com.smbms.util.StatusCode;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;


@RestController
@RequestMapping("/user")
@Api(tags = "用户模块的api")
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private JwtUtil jwtUtil;
    @Autowired
    private HttpServletRequest request;


    @PostMapping("/login")
    @ApiOperation(value = "用户登录")
    public Result login(@RequestBody User user) throws Exception{
        User userLogin = userService.login(user.getUsercode(), user.getUserpassword());
        if(userLogin!=null) {
            //生成token
            String token=jwtUtil.createJWT(userLogin.getId()+"",userLogin.getUsername(),userLogin.getUserrole()+"");
            System.out.println("token:"+token);
            return new Result(true,StatusCode.SUCCESS,"操作成功",token);
        }

        return new Result(false,StatusCode.ACCESSERROR,"操作失败");
    }

    @PostMapping("/add")
    @ApiOperation("用户增加")
    public Result addUser(@RequestBody User user) throws Exception{
        if(userService.addUser(user)){
            return new Result(true,StatusCode.SUCCESS,"操作成功");

        };
        return new Result(false,StatusCode.ERROR,"操作失败");

    }

    @GetMapping("/find")
    @ApiOperation("搜索用户")
    @ApiImplicitParam(value = "用户实体类",name = "user",required = true)
    public Result find(@RequestBody User user) throws Exception {
        List<User> userList = userService.showUserListByName(user.getUsername());
        return new Result(true,StatusCode.SUCCESS,"ok",userList);
    }

    @GetMapping("/{id}")
    @ApiOperation("根据id查找用户")
    @ApiImplicitParam(value = "用户id",name = "id",required = true)
    public Result showUserByUserId(@PathVariable Long id) throws Exception{
        User user = userService.showUserInfoById(id);
        if(user!=null)
          return new Result(true,StatusCode.SUCCESS,"ok",user);
        return new Result(false,StatusCode.ERROR,"no");
    }


    @DeleteMapping("/{id}")
    public Result del(@PathVariable Long id) throws Exception{
       //获取role
        Object role = request.getAttribute("role");
        if(!"admin".equals(role)){
            throw new Exception("只有管理员才能删除");
        }


        boolean isOk = userService.del(id);
        return new Result(isOk,isOk?StatusCode.SUCCESS:StatusCode.ERROR,"操作成功");
    }




    @PutMapping("/save/{id}")
    public Result saveUser(@RequestBody User user,@PathVariable Long id) throws Exception{
        user.setId(id);
        //调用服务层代码，根据id修改用户信息
        boolean isOk = userService.updateUser(user);
       return new Result(isOk,isOk?StatusCode.SUCCESS:StatusCode.ERROR,"操作成功");
    }

    @GetMapping("/list/{page}/{size}")
    public Result showUserList(@PathVariable Integer page,@PathVariable Integer size) throws Exception{

        PageResult<User> pageResult = userService.showUserListByPage(page, size);
        return new Result(true,StatusCode.SUCCESS,"操作完成",pageResult);
    }


}
